//40.组合总和II
//https://leetcode.cn/problems/combination-sum-ii/
class Solution {
    vector<vector<int>> ret;
    vector<int> path;
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        int n = candidates.size();
        sort(candidates.begin(), candidates.end());
        dfs(candidates,target,0);
        return ret;
    }


    void dfs(vector<int>& candidates, int target, int i)
    {
        if (0 == target) ret.push_back(path);
        int n = candidates.size();
        for (int k = i; k < n && target - candidates[i] >= 0; ++k)
        {
            if(k > i && candidates[k]==candidates[k-1]) continue;
            path.push_back(candidates[k]);
            dfs(candidates, target-candidates[k], k + 1);
            path.pop_back();
        }
    }
};